package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.TaxationDAO;
import lk.icta.core.entity.Taxation;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;

/**
 * @author Supun Viraj Rathnayaka
 */
public class TaxationDAOImpl extends BaseDAO implements TaxationDAO {

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Taxation> getAllTaxationData() {
        logger.debug("Retrieving All Taxation Data");
        Query q = em.createNamedQuery("getAllTaxData");
        return q.getResultList();
    }
    
    @Transactional(propagation = Propagation.REQUIRED)
    public void addTaxation(Taxation taxation, User user) {
        em.persist(taxation);     
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Taxation getPreviousTaxation() {
        Query q = em.createNamedQuery("getLastTaxation");
        List<Taxation> t =  q.setMaxResults(1).getResultList();
        if(t.size() == 1){
            return t.get(0);
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateTaxation(Taxation t, User user) {
        em.merge(t);
    }
}
